package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.pojo.User;
import com.zjitc.service.UserService;
import java.util.Date;
import java.util.Set;
import javax.naming.Name;
import javax.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;


/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/28 14:30
 * @description:
 */
@Controller
@RequestMapping("/user")
public class UserController {

  @Autowired
  private Jedis jedis;

  @Autowired
  private UserService userService;

  /**
   * 用户登录
   * @param username
   * @param password
   * @param session
   * @return
   */
  @RequestMapping(method=RequestMethod.POST,value = "/login")
  @ResponseBody
  public ServerResponse login(
      @RequestParam(name = "username",required = false) String username,
      @RequestParam(name ="password",required = false) String password,
      HttpSession session){
     /**
      * if success->ServerResponse<User>
      * if error  ->ServerResponse.message=密码错误
      * */
     ServerResponse<User> response=userService.login(username,password);
     if(response.success()){
       //save to session
       session.setAttribute("user",response.getData());
     }
     return response;
  }

  /**
   * 用户注册
   * @param user
   * @return
   */
  @RequestMapping(method = RequestMethod.POST, value = "/register")
  @ResponseBody
  public ServerResponse register(User user){
        user.setRole(0);
    user.setCreate_time(new Date());
    user.setUpdate_time(new Date());
     ServerResponse<User> response= userService.insert(user);
     return response;
  }

  @RequestMapping(method = RequestMethod.POST, value="/check_valid")
  @ResponseBody
  public ServerResponse check(
          @RequestParam(name="str",required = false)String str,
          @RequestParam(name="type",required = false)String type){
    ServerResponse<User> response=userService.check(str,type);
    return response;
  }


  /**
   * 获取登录用户信息
   * @param session
   * @return
   */
  @RequestMapping(method = RequestMethod.POST, value="/get_user_info")
  @ResponseBody
  public ServerResponse getUser(HttpSession session){
    ServerResponse<User> response=userService.getUser(session);
    return response;
  }

  @RequestMapping(method = RequestMethod.POST, value="/forget_get_question")
  @ResponseBody
  public ServerResponse forget(User user){
    ServerResponse<User> response=userService.getUserQuestion(user);
    return response;
  }


  @RequestMapping(method = RequestMethod.POST,value="/forget_check_answer")
  @ResponseBody
  public ServerResponse checkAnswer(String username,String question,String answer){
    return null;
  }

  /**
   * 重置密码
   *
   * @return
   */

  @RequestMapping(method = RequestMethod.GET,value = "/forget_reset_password")
  @ResponseBody
  public ServerResponse forgetPassWord(String username,String passwordNew, String forgetToken){

    return null;
    }


  /**
   * 登录中状态重置密码
   * @param passwordOld
   * @param passwordNew
   * @return
   */
  @RequestMapping(method = RequestMethod.GET,value = "/reset_password")
  @ResponseBody
  public ServerResponse resetPassword(User user,HttpSession session,String passwordOld,String passwordNew){
    ServerResponse<User> response=userService.resetPassword(user,session,passwordOld,passwordNew);
    return  response;
  }

  /**
   * 登录状态更新个人信息
   * @param user
   * @param session
   * @return
   */
  @RequestMapping(method = RequestMethod.GET, value="/update_information")
  @ResponseBody
  public ServerResponse update(User user,HttpSession session){
    ServerResponse<User> response=userService.update(user,session);
    return response;
  }

  @RequestMapping("/logout")
  @ResponseBody
  public ServerResponse logout(HttpSession session,User user){
      session.getAttribute("user");
      session.removeAttribute("user");
      if(session==null){
        return ServerResponse.createSuccess("退出成功");
      }else {
        return ServerResponse.createError("服务端异常");
      }

  }

}